package com.lmpop.trade.ts_categoryattr;

import com.lmpop.frame.QueryList;
import com.lmpop.frame.SysMap;
import com.lmpop.frame.TradeInterf;
import com.lmpop.frame.dao.DbmInterf;
import com.lmpop.frame.exp.ApplicationException;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

public class Ts_categoryattrInfo
  implements TradeInterf
{
  QueryList queryList;

  public Ts_categoryattrInfo()
  {
    this.queryList = new QueryList();
  }

  public SysMap insert(DbmInterf dbmMgr, SysMap map)
  {
    Ts_categoryattr ts_categoryattr = new Ts_categoryattr();
    ts_categoryattr.setClass_id(map.getString("class_id"));
    ts_categoryattr.setAttr_id(map.getString("attr_id"));
    ts_categoryattr.setAttr_name(map.getString("attr_name"));
    ts_categoryattr.setAttr_no(map.getInt("attr_no"));
    ts_categoryattr.setDefault_tag(map.getString("default_tag"));
    ts_categoryattr.setDefault_value(map.getString("default_value"));
    ts_categoryattr.setEnable_tag(map.getString("enable_tag"));
    ts_categoryattr.setCon_type(map.getString("con_type"));
    ts_categoryattr.setEn_name(map.getString("en_name"));

    String result_code = "0";
    try {
      insert(dbmMgr, ts_categoryattr);
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void insert(DbmInterf dbmMgr, Ts_categoryattr ts_categoryattr)
    throws ApplicationException
  {
    dbmMgr.update("insertTs_categoryattr", ts_categoryattr);
  }

  public SysMap delete(DbmInterf dbmMgr, SysMap map)
  {
    String pkid_string = map.getString("pkid");
    String result_code = "0";
    try {
      if (!pkid_string.equals("")) {
        String[] pkStr = pkid_string.split("\\|");
        for (int i = 0; i < pkStr.length; i++) {
          if (pkStr[i].equals("")) continue; delete(dbmMgr, pkStr[i]);
        }
      }
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void delete(DbmInterf dbmMgr, String pkid)
    throws ApplicationException
  {
	  //delete from ts_categoryattr where attr_id=#value#
    dbmMgr.update("deleteTs_categoryattr", pkid);
  }

  public SysMap update(DbmInterf dbmMgr, SysMap map)
  {
    Ts_categoryattr ts_categoryattr = new Ts_categoryattr();
    ts_categoryattr.setClass_id(map.getString("class_id"));
    ts_categoryattr.setAttr_id(map.getString("attr_id"));
    ts_categoryattr.setAttr_name(map.getString("attr_name"));
    ts_categoryattr.setAttr_no(map.getInt("attr_no"));
    ts_categoryattr.setDefault_tag(map.getString("default_tag"));
    ts_categoryattr.setDefault_value(map.getString("default_value"));
    ts_categoryattr.setEnable_tag(map.getString("enable_tag"));
    ts_categoryattr.setCon_type(map.getString("con_type"));
    ts_categoryattr.setEn_name(map.getString("en_name"));

    String result_code = "0";
    try {
      update(dbmMgr, ts_categoryattr);
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void update(DbmInterf dbmMgr, Ts_categoryattr ts_categoryattr)
    throws ApplicationException
  {
    dbmMgr.update("updateTs_categoryattr", ts_categoryattr);
  }

  public List getListByPage(Object obj, int start, int limit)
  {
    Ts_categoryattr ts_categoryattr = (Ts_categoryattr)obj;
    if (start == 0)
      start = 0;
    else {
      start = (start - 1) * limit;
    }
    ts_categoryattr.setStart(Integer.valueOf(start));
    ts_categoryattr.setLimit(Integer.valueOf(limit));
    return this.queryList.getList("getTs_categoryattrList", ts_categoryattr);
  }

  public int getCountByObj(Object obj)
  {
    Ts_categoryattr ts_categoryattr = (Ts_categoryattr)obj;

    List list = this.queryList.getList("getCountByObjTi_custlink", ts_categoryattr);
    int count = 0;
    if ((list != null) && (list.size() > 0)) {
      Hashtable newsMap = (Hashtable)list.get(0);
      if ((newsMap.get("count") != null) && (!newsMap.get("count").toString().equals(""))) {
        count = Integer.parseInt(newsMap.get("count").toString());
      }
    }
    return count;
  }

  public List getListByPk(String pk)
  {
    return this.queryList.getList("getTs_categoryattrByPk", pk);
  }

  public List getListByClassId(String ClassId)
  {
    return this.queryList.getList("getTs_categoryattrByClassId", ClassId);
  }

  public SysMap categoryAttrSort(DbmInterf dbmMgr, SysMap map)
  {
    Ts_categoryattr ts_categoryattr = new Ts_categoryattr();
    String pkid_string = map.getString("sort");
    String result_code = "0";
    try {
      if (!pkid_string.equals("")) {
        String[] pkStr = pkid_string.split("\\|");
        for (int i = 0; i < pkStr.length; i++)
        {
          if (pkStr[i].equals(""))
            continue;
          String[] sort = pkStr[i].split(",");
          if (sort.length == 1)
          {
            ts_categoryattr.setAttr_id(sort[0]);
            ts_categoryattr.setAttr_no(0);
            updateSort(dbmMgr, ts_categoryattr);
          }
          if (sort.length != 2)
            continue;
          ts_categoryattr.setAttr_id(sort[0]);
          ts_categoryattr.setAttr_no(Integer.parseInt(sort[1]));
          updateSort(dbmMgr, ts_categoryattr);
        }
      }
    }
    catch (ApplicationException e)
    {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void updateSort(DbmInterf dbmMgr, Ts_categoryattr ts_categoryattr) throws ApplicationException {
    dbmMgr.update("updateTs_categoryAttrSort", ts_categoryattr);
  }

  public SysMap deleteByClassId(DbmInterf dbmMgr, SysMap map)
  {
    String pkid_string = map.getString("cat_ids");
    String result_code = "0";
    try {
      if (!pkid_string.equals("")) {
        String[] pkStr = pkid_string.split("\\|");
        for (int i = 0; i < pkStr.length; i++) {
          if (pkStr[i].equals("")) continue; deleteByClassId(dbmMgr, pkStr[i]);
        }
      }
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void deleteByClassId(DbmInterf dbmMgr, String pkid)
    throws ApplicationException
  {
    dbmMgr.update("deleteTs_categoryattrByClassId", pkid);
  }

  public String getSelectItems(String attr_id, String attr_desc)
  {
    String selectItems = "";
    List list = getListByPk(attr_id);

    String sed_value = "";
    if (!attr_desc.equals(""))
    {
      String[] attrDs = attr_desc.split(",");
      for (int i = 0; i < attrDs.length; i++)
      {
        if (attrDs[i].indexOf(attr_id) <= -1)
          continue;
        String[] attr = attrDs[i].split("\\|");
        if (attr.length <= 1)
          break;
        sed_value = attr[1]; break;
      }

    }

    if ((list != null) && (list.size() > 0))
    {
      Hashtable objMap = (Hashtable)list.get(0);

      String default_value = "";

      if (objMap.get("default_value") != null)
      {
        default_value = objMap.get("default_value").toString();
      }

      String[] dValue = default_value.split("\\|");

      for (int i = 0; i < dValue.length; i++)
      {
        if ((!sed_value.equals("")) && (sed_value.equals(dValue[i])))
        {
          selectItems = selectItems + "<option value=" + dValue[i] + "  selected>" + dValue[i] + "</option>";
        }
        else
        {
          selectItems = selectItems + "<option value=" + dValue[i] + ">" + dValue[i] + "</option>";
        }
      }
    }
    return selectItems;
  }

  public String getInputItems(String attr_id, String attr_desc)
  {
    String inp_value = "";
    if (!attr_desc.equals(""))
    {
      String[] attrDs = attr_desc.split(",");
      for (int i = 0; i < attrDs.length; i++)
      {
        if (attrDs[i].indexOf(attr_id) <= -1)
          continue;
        String[] attr = attrDs[i].split("\\|");
        if (attr.length <= 1)
          break;
        inp_value = attr[1]; break;
      }

    }

    return inp_value;
  }

  public String getCheckboxItems(String attr_id, String attr_desc)
  {
    String cb_value = "";
    if (!attr_desc.equals(""))
    {
      String[] attrDs = attr_desc.split(",");
      for (int i = 0; i < attrDs.length; i++)
      {
        if (attrDs[i].indexOf(attr_id) <= -1)
          continue;
        cb_value = attrDs[i];
        break;
      }

    }

    return cb_value;
  }

  public String getAttrNameById(String attr_id)
  {
    List alist = getListByPk(attr_id);
    String attr_name = "";
    if ((alist != null) && (alist.size() > 0))
    {
      Hashtable aMsp = (Hashtable)alist.get(0);
      if (aMsp.get("attr_name") != null)
      {
        attr_name = aMsp.get("attr_name").toString();
      }
    }
    return attr_name;
  }

  public String getCatAttrFromStr(String attr_desc, int num)
  {
    StringBuffer attrBuffer = new StringBuffer();
    if (!attr_desc.equals(""))
    {
      String[] attrDs = attr_desc.split(",");
      for (int i = 0; i < attrDs.length; i++)
      {
        if (num == i) break;
        if (attrDs[i].equals(""))
          continue;
        String[] cb_value = attrDs[i].split("\\|");
        if (cb_value.length <= 1)
          continue;
        String attr_name = getAttrNameById(cb_value[0]);
        attrBuffer.append(attr_name + ":" + cb_value[1] + " ,");
      }

    }

    return attrBuffer.toString();
  }

  public Map getAllCatAttr() {
    List list = this.queryList.getList("getAllCatAttr");
    Map attrMap = new Hashtable();
    if ((list != null) && (list.size() > 0)) {
      Hashtable map = new Hashtable();
      for (int i = 0; i < list.size(); i++) {
        map = (Hashtable)list.get(i);
        String attr_id = ""; String attr_name = "";
        if (map.get("attr_id") != null) attr_id = map.get("attr_id").toString();
        if (map.get("attr_name") != null) attr_name = map.get("attr_name").toString();
        attrMap.put(attr_id, attr_name);
      }
    }
    return attrMap;
  }
}